package com.gin.kotlin.no_gay_alliance.fragment.main.me

import android.view.View
import com.gin.kotlin.no_gay_alliance.adapter.me.BlockConfigAdapter
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.callback.RCallback
import com.gin.kotlin.no_gay_alliance.databinding.FragmentMainMeBlockConfigBinding
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.BlockConfigProvider
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import com.gin.nga.response.user.BlockConfig
import com.gin.nga.response.user.BlockConfigBody
import com.gin.nga.response.user.TinyUserInfo
import retrofit2.Call
import retrofit2.Response

class BlockConfigFragment : AbstractBindingFragment<FragmentMainMeBlockConfigBinding>(), RCallback<BlockConfigBody, Nothing> {
    private lateinit var keywordAdapter: BlockConfigAdapter
    private lateinit var userAdapter: BlockConfigAdapter

    override fun onCreateView() {
        val b = this.b ?: return
        this.userAdapter = BlockConfigAdapter(requireContext(), handler!!)
        this.keywordAdapter = BlockConfigAdapter(requireContext(), handler!!)

        b.recyclerViewBlockUser.adapter = userAdapter
        b.recyclerViewBlockKeyword.adapter = keywordAdapter
        b.swipeRefreshLayout.setOnRefreshListener { request(true) }

        b.btSave.setOnClickListener {
            val config = BlockConfig()
            config.users = this.userAdapter.data.map { TinyUserInfo(username = it) }.toMutableList()
            config.keywords = this.keywordAdapter.data

            App.INSTANCE.ngaApi.accountApi.blockConfigSet(config).enqueue(object : NgaCallback<List<String>> {
                override fun onSuccess(
                    call: Call<NgaResponse<List<String>>>,
                    response: Response<NgaResponse<List<String>>>,
                    body: NgaResponse<List<String>>?,
                    data: List<String>?
                ) {
                    data?.get(0)?.also { toast(it) }
                    handler?.post { request(true) }
                }

                override fun onError(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, errorMessage: String) {
                    toastLong(errorMessage)
                }
            })
        }
        request()
    }

    private fun request(ignoreCache: Boolean = false) {
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = true
        b.tvWarning.visibility = View.GONE
        BlockConfigProvider.getInstance().enqueue(ignoreCache = ignoreCache, callback = this)
    }

    override fun onSuccess(data: BlockConfigBody, ignoreCache: Boolean) {
        b?.swipeRefreshLayout?.isRefreshing = false
        this.userAdapter.replaceData(data.data?.users?.mapNotNull { it.username })
        this.keywordAdapter.replaceData(data.data?.keywords)

        // 更新屏蔽配置
        App.INSTANCE.blockConfig = data.data
    }

    override fun onError(errorMessage: String) {
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = false
        b.tvWarning.visibility = View.VISIBLE
        b.tvWarning.text = errorMessage
    }
}